import { ref, watch } from 'vue';
import { expertInfoDetailApi } from '/@/api/expertDatabase/expert';
import { useRoute } from 'vue-router';
import { ElLoading } from 'element-plus';
import { getRouterParams } from '/@/utils/hyFunc';

export function useDetail() {
	const route = useRoute();
	const routerParams: any = ref('');
	const basicData = ref({
		expertInfoId: '',
		expertName: '',
		expertTel: '',
		origin: '',
		ethnicGroup: '',
		identityNumber: '',
		gender: '',
		birthDate: '',
		politicalLandscape: '',
		officePhone: '',
		bankcardNumber: '',
		accountBank: '',
		expertSources: '',
		expertType: '',
		headlessPhoto: '',
		headlessPhotoFiles: [],
		expertTypeArr: [],
		genderName: '',
		ethnicGroupName: '',
		politicalLandscapeName: '',
		expertSourcesName: '',
		expertTypeName: '',
		unitUnifiedSocialCreditcode: '',
		unitName: '',
		politicalLandscapeArr: [],
		blacklist: '',
		status: '1',
	});
	const careerData = ref({
		unifiedSocialCreditcode: '',
		workPlace: '',
		workStartTime: '',
		yearsOfService: '',
		incumbentStatus: '',
		timeOfRetirement: '',
		jobTitle: '',
		jobLevel: '',
		office: '',
		workExperience: '',
		projectExperience: '',
		incumbentStatusName: '',
		jobTitleName: '',
	});
	const educationData = ref({
		graduationSchool: '',
		majorsStudied: '',
		education: '',
		academicDegree: '',
		isStudy: '',
		isStudySchool: '',
		diplomaFiles: [],
		degreeCertificate: '',
		diploma: '',
		degreeCertificateFiles: [],
		graduationSchoolName: '',
		majorsStudiedName: '',
		academicDegreeName: '',
		educationName: '',
		isStudyName: '',
	});
	const expertiseData = ref({
		industryFields: '',
		professionalFields: '',
		avoidanceUnitsArr: [],
		recognitionRewards: '',
		direction: '',
		professionalTitleFiles: [],
		otherProofFiles: [],
		industryFieldsArr: [],
		professionalFieldsArr: [],
		otherProof: '',
		professionalTitle: '',
		industryFieldsName: '',
		professionalFieldsName: '',
	});
	const getDetailData = (expertInfoId: string) => {
		const loading = ElLoading.service({
			lock: true,
			text: '获取数据中...',
		});
		expertInfoDetailApi({ expertInfoId })
			.then((res: any) => {
				basicData.value = mergeInfoData(basicData.value, res);
				careerData.value = mergeInfoData(careerData.value, res);
				educationData.value = mergeInfoData(educationData.value, res);
				expertiseData.value = mergeInfoData(expertiseData.value, res);
			})
			.finally(() => {
				loading.close();
			});
	};
	//设置标题
	const title = ref('');
	const setTtile = (type: any) => {
		if (type === 'add') {
			title.value = '新增';
		}
		if (type === 'edit') {
			title.value = '编辑';
		}
		if (type === 'submit') {
			title.value = '提交';
		}
		if (type === 'view') {
			title.value = '详情';
		}
		if (type === 'approve') {
			title.value = '审核';
		}
	};
	// 合并两个对象，并处理特殊字段
	const mergeInfoData = (target: any, source: any) => {
		let result: any = {};
		Object.keys(target).forEach((key) => {
			result[key] = source[key];
		});
		return result;
	};
	watch(
		[() => route.query],
		() => {
			routerParams.value = getRouterParams();
			if (routerParams.value.operateType !== 'add') {
				getDetailData(routerParams.value.expertInfoId);
			}
			setTtile(routerParams.value?.operateType);
		},
		{ immediate: true, deep: true }
	);
	return {
		getDetailData,
		title,
		routerParams,
		basicData,
		careerData,
		educationData,
		expertiseData,
	};
}
